Page 89 - 2629_Devagiri_C-8
P. 89
For example, range(2, 6) gives: [2, 3, 4, 5] (6 is excluded).
step: This optional value defines how much the value should increase or decrease each time.
The default step value is 1, which means the numbers go up one at a time. You can also use a
negative step value to create a descending sequence.
For example: range(10, 2, -2) gives [10, 8, 6, 4].
range(2, 8, 3) gives [2, 5].
FOR LOOP IN PYTHON
The for loop repeats a block of statements a fixed Start
In the previous class, you learnt that Python is a high-level, interpreted programming language number of times. It is commonly used to go through
known for its simplicity and readability. You also explored how to download and install it on your (iterate over) a sequence like a list, a string or a range
system and wrote and executed simple short programs in Python. In this chapter, you are going of numbers. It can be specified using a sequence or a False Loop condition
to move onwards with Python basics and write more advanced programs. range of values.
increment/
Syntax of the for loop: True decrement
PYTHON ITERATIVE STATEMENTS for variable in sequence: Code to be executed
Statement block to be repeated
Iteration means repeating a set of instructions again and again. It is a flow of control that allows The for loop variable iterates over the set of values given
a part of the code to run multiple times without writing it again each time. In many programs, we in the sequence, repeating the loop body once for each Stop
often need to repeat certain actions. Instead of copying the same code many times, we use loops item. Therefore, the loop header itself defines how many times the loop is going to repeat. For
to do it automatically. each iteration, the loop variable takes up the next value from the set and executes the loop body
Using loops helps save time, reduce mistakes and make the code shorter and cleaner. one more time. The process goes on till the last item of the set has been used.
Python provides two main types of loops: the for loop, which repeats a block of code for each item Program 1 To print each character of a string.
in a sequence or for a specific number of iterations and the while loop, which repeats a block of
code as long as a condition is true. Program1.py Output
range() function File Edit Format Run Options Window Help H
The range() function is a built-in function in Python that generates a sequence of numbers. for i in "Hello": e
The range() function is most commonly used print(i) l
with the for loop to repeat tasks a specific l
number of times. o
Python has no limit on variable name length, but
Syntax of the range() function: line lengths should be kept under 79 characters
range(start, stop, step) for better readability. Syntax of the for loop with the range():
where, for variable in range(start, stop, step):
start: This is the optional value where the sequence begins. If you don’t specify it, Python will Statement block to be repeated
automatically start from 0. The for loop initialises the variable with the start value. With each iteration, the variable
For example, range(5) is treated as range(0, 5) and generates: [0, 1, 2, 3, 4]. changes based on the step value—increment or decrement. This continues until it reaches the
stop value (which is not included). The code inside the loop runs once for each value. This makes
stop: This is the mandatory value at which the sequence ends, but it is not included in the
result. it easy to repeat tasks in a clear and controlled way when you know how many times you want
to repeat them.
87
Networks Around Us

